package com.zzyl.serve.controller;

import java.security.Provider;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONUtil;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.*;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.vo.DeviceDetailVo;
import com.zzyl.serve.vo.DeviceReportDataVo;
import com.zzyl.serve.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author liuyp
 * @date 2025-08-22
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private IoTDAClient client;

    /**
     * 查询设备列表
     */
    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('serve:device:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("设备查询条件") Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 物联网中维护了产品列表信息，需要同步到redis中以供使用注册、分页查询
     * @return
     */
    @ApiOperation("从物联网平台同步产品列表")
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
        return AjaxResult.success();
    }


    @GetMapping("/allProduct")
    @ApiOperation("查询所有的产品列表")
    public R<List<ProductVo>> allProduct(){
        String listStr = (String)redisTemplate.opsForValue().get("productList");
        if(StringUtils.isEmpty(listStr)){
            throw new BaseException("查询产品列表失败");
        }

        return R.ok(JSONUtil.toList(listStr, ProductVo.class));
    }

    @ApiOperation("注册设备")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto dto){
        deviceService.register(dto);
        return AjaxResult.success();
    }

    @ApiOperation("查看设备所有信息")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> deviceDetail(@PathVariable String iotId){

        return deviceService.deviceDetail(iotId);
    }

    @ApiOperation("查看设备上报的数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<DeviceReportDataVo>> queryServiceProperties(@PathVariable String iotId){

        List<DeviceReportDataVo> voList = deviceService.queryServiceProperties(iotId);

        return R.ok(voList);
    }

    @ApiOperation("修改设备")
    @PutMapping
    public AjaxResult updateDevice(@RequestBody DeviceDto dto){
        deviceService.updateDevice(dto);

        return AjaxResult.success();
    }

    @ApiOperation("删除设备")
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable String iotId){
        deviceService.deleteDevice(iotId);
        return AjaxResult.success();
    }
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询设备的物模型属性")
    public R<List<ServiceCapability>> queryProduct(@PathVariable("productKey") String productKey){
        List<ServiceCapability> list = deviceService.queryProduct(productKey);
        return R.ok(list);
    }
}
